package com.team06.dao;

import com.team06.pojo.Ckoe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class CkoeDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(CkoeDao.class);
    private final JdbcTemplate jdbcTemplate;

    public CkoeDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 向 MSG_DFME_CKOE 表插入数据
     * @param entity 封装了要插入数据的 Ckoe 实体对象
     */
    public void insertMessage(Ckoe entity) {
        // 定义插入数据的SQL语句，列名与数据库表 MSG_DFME_CKOE 保持一致
        String sql = "INSERT INTO MSG_DFME_CKOE (" +
                "SENDER, RECEIVER, SEQUENCE, DTIME, MTYPE, STYPE, " +
                "DFLT_FLID, DFLT_FFID, DFLT_FIDE, DFLT_FATT, DFLT_STAT, " +
                "DFLT_ISTA, DFLT_FCRE, DFLT_MSTA, DFLT_MIST, DFLT_MCRE) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            // 使用 JdbcTemplate 执行更新操作，将实体对象的属性值作为参数传入
            jdbcTemplate.update(sql,
                    entity.getMetaSnDR(),
                    entity.getMetaRCVR(),
                    entity.getMetaSEQN(),
                    entity.getMetaDDTM(),
                    entity.getMetaTYPE(),
                    entity.getMetaSTYP(),
                    entity.getDfltFLID(),
                    entity.getDfltFFID(),
                    entity.getDfltFIDE(),
                    entity.getDfltFATT(),
                    entity.getDfltSTAT(),
                    entity.getDfltISTA(),
                    entity.getDfltFcre(),
                    entity.getDfltMsta(),
                    entity.getDfltMist(),
                    entity.getDfltMcre()
            );
            LOGGER.info("Ckoe 数据插入成功");
        } catch (Exception e) {
            LOGGER.error("Ckoe 数据插入失败", e);
            // 捕获异常后抛出运行时异常，让调用者感知插入操作失败
            throw new RuntimeException("Failed to insert Ckoe message into database", e);
        }
    }
}